package com.office.shop.by;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.office.shop.by.components.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.view.Window;
import java.util.ArrayList;
import java.util.HashMap;

public class BalanceLogActivity extends Activity {

    private ProgressDialog pd;
    static ArrayList<HashMap<String,String>> list =new ArrayList<HashMap<String,String>>();
    private static String action = "/android/balancelog";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.loading);
        Refresh();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
           MenuInflater inflater = getMenuInflater();
           inflater.inflate(R.menu.balance_menu, menu);
           return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.mmRefresh:
            this.Refresh();
            return true;
            case R.id.mmExit:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public void Refresh()
    {
       pd = ProgressDialog.show(this, "", getString(R.string.DataUpdate), true, false);
       new JSONParser().execute();
    }

    public class JSONParser extends AsyncTask<String, Void, JSONObject>
    {
        //Фоновая операция
        protected JSONObject doInBackground(String arg[]) {
          JSONObject output = new JSONObject();
          try
          {
            if(CAuth.login( CAccounts.logins[MultiAccActivity.current_acc],
                           CAccounts.passwords[MultiAccActivity.current_acc]))
               output = CHttp.postJSON((CConfig.serverUrl + BalanceLogActivity.action), CAuth.getLogin());
            else
               output = null;
          }
          catch(Exception e)
          {
            e.printStackTrace();
          }
          return output;
        }


        //Событие по окончанию парсинга
        protected void onPostExecute(JSONObject output) {
            if(output != null)
            {
                list = new ArrayList<HashMap<String,String>>();
                setContentView(R.layout.balancelog);
                ListView lvBalanceLog = (ListView)findViewById(R.id.lvBalanceLog);
                SimpleAdapter simpleAdapter = new SimpleAdapter(BalanceLogActivity.this,
                                list,
                                R.layout.balancelog_row,
                                new String[] {"sum","date"},
                                new int[] {R.id.text1,R.id.text2});
               populateList(output);
               simpleAdapter.notifyDataSetChanged();
               lvBalanceLog.setAdapter(simpleAdapter);
               lvBalanceLog.setChoiceMode(ListView.CHOICE_MODE_NONE);

            }
            else
            {
                setContentView(R.layout.login);
            }

            pd.dismiss();
        }
    }

    private void populateList(JSONObject output) {
        int index = 0;
        try {
            JSONArray balanceLog = output.getJSONArray("balancelog").getJSONArray(0);
            while(index < balanceLog.length())
            {
                HashMap<String,String> temp = new HashMap<String,String>();
                JSONObject json_data = balanceLog.getJSONObject(index);
                temp.put("sum", CFormat.numberFormat(json_data.getString("sum").toString()));
                temp.put("date", json_data.getString("date").toString());
                list.add(temp);
                index++;
            }
        } catch (JSONException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


}

}